import Vue from 'vue'

function getCurrentDateStr() {
	const now = new Date();
	var currentTime = now.getFullYear() + '-' +
		('0' + (now.getMonth() + 1)).slice(-2) + '-' +
		('0' + now.getDate()).slice(-2) + ' ' +
		('0' + now.getHours()).slice(-2) + ':' +
		('0' + now.getMinutes()).slice(-2) + ':' +
		('0' + now.getSeconds()).slice(-2);
	return currentTime
}
function dateToStr(timestamp) {
// 将时间戳转换为Date对象
var date = new Date(timestamp);
// 格式化日期和时间
// date.getFullYear() + '-' +
var formattedDate = 
    ('0' + (date.getMonth() + 1)).slice(-2) + '/' +
    ('0' + date.getDate()).slice(-2) 
   
	return formattedDate;
}
//获取当前时间几天后的时间戳
function getInAFewDays(day){
	// 获取当前时间的Date对象
	let now = new Date();
	// 获取当前时间的时间戳
	let nowTimestamp = now.getTime();
	// 计算2天后的时间戳
	let twoDaysLaterTimestamp = nowTimestamp + day * 24 * 60 * 60 * 1000; // 2天 = 2 * (24小时 * 60分钟 * 60秒 * 1000毫秒)
	console.log('当前时间戳: ', nowTimestamp);
	console.log(day,'天后的时间: ', twoDaysLaterTimestamp);
	return twoDaysLaterTimestamp;
}
//
function isQRCodeExpired(releaseTimestamp){
	var day = 7; 
	// 获取当前时间的Date对象
	let now = new Date();
	// 获取当前时间的时间戳
	// 计算发布时间后的7天时间
	let resTimestamp = releaseTimestamp + day * 24 * 60 * 60 * 1000; // 2天 = 2 * (24小时 * 60分钟 * 60秒 * 1000毫秒)
	
	let nowTimestamp = now.getTime();
	var res = nowTimestamp - resTimestamp;
	console.log('当前时间戳: ', nowTimestamp);
	console.log('发布后7天的时间: ', resTimestamp);
	if(res > 0 ){
		return false;
	}
	return true;
}

function chooseImage() {
	return new Promise((resolve, reject) => {

		uni.chooseImage({
			count: 1, // 默认9
			sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
			sourceType: ['album'], // 从相册选择
			success: (res) => {
				const tempFilePaths = res.tempFilePaths;

				resolve(tempFilePaths[0]);

			},
			fail: (error) => {
				reject(error);
			}
		});
	});
}


function uploadImage(filePath) {
	console.log('执行上传图片===', filePath);
	return new Promise((resolve, reject) => {
		const dateStr = this.getCurrentDateStr() + '.png';
		uni.showLoading({
			title: '图片上传中...'
		});
		uniCloud.uploadFile({
			filePath: filePath,
			cloudPath: dateStr,
			onUploadProgress: function(progressEvent) {
				console.log('长传图进度', progressEvent);
				var percentCompleted = Math.round(
					(progressEvent.loaded * 100) / progressEvent.total
				);
			},
			success(res) {
				console.log('上传成功', res);
				if (res.success == true) {
					var imageUrl = res.fileID;
					resolve(imageUrl);
				} else {
					reject('上传失败');
				}

			},
			fail(err) {
				reject('上传失败');
				console.error('上传失败', err);
				uni.showToast({
					title: err.toString(),
					icon: 'none'
				})
			},
			complete() {
				uni.hideLoading();
			}
		});
	});
}


export {
	getCurrentDateStr,
	dateToStr,
	uploadImage,
	chooseImage,
	getInAFewDays,
	isQRCodeExpired

}